using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 这种做法不好 临时做法 应该让服务器判断 不能直接发给客户端正确答案
namespace CJ.XNFZ.TEST {

    [System.Serializable]
    public class Question {
        public string id  = "";
        public string stem  = "";
        public string[] alternatives  = null;
        public int correct =0;
    }

    [System.Serializable]
    public class MCQ {
        public string name = "";
        public string id = "";
        public string version = "";
        public Question[] questions  = null;
    }

    public class TestsystemHelper : MonoBehaviour {

        public string file = "test.json";
        public MCQ config;

        public bool autogetconfig = false;

        void Start () {
            if (autogetconfig)
                GetConfig ();
        }

        public void GetConfig (Action ok = null, Action<string> error = null) {
            XNFZTemplateHelper.Instance.GetConfig (
                file,
                (string json) => {
                    config = JsonUtility.FromJson<MCQ> (json);
                    if (ok != null) ok ();
                },
                (string e) => {
                    if (error != null) error ("获取配置文件错误");
                    Debug.Log("a1");
                }
            );
        }

        public List<Question> getRandom (int count) {
            var t = new List<Question> (config.questions);
            Shuffle (t);
            return t.GetRange (0, count > t.Count?t.Count : count);
        }

        static List<T> Shuffle<T> (List<T> list) {
            for (int i = 0; i < list.Count; i++) {
                int k = UnityEngine.Random.Range (0, i);
                T value = list[k];
                list[k] = list[i];
                list[i] = value;
            }
            return list;
        }

    }
}